package com.hbu.product.hbumallproduct.controller;

import com.hbu.common.utils.R;
import com.hbu.product.hbumallproduct.entity.BrandEntity;
import com.hbu.product.hbumallproduct.service.BrandService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * Ʒ?
 *
 * @author zhaoxiaohan
 * @email 13315217827@163.com
 * @date 2022-10-27 22:52:06
 */
@RestController
@RequestMapping("hbumallproduct/brand")
@AllArgsConstructor
public class BrandController {

    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok().put("page", brandService.queryPage(params));
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        return R.ok().put("brand", brandService.getById(brandId));
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@Validated @RequestBody BrandEntity brand) {
        return R.result(brandService.save(brand), "保存");
    }

    /**
     * 修改,因为存在 brand_category_relation 因此在修改brand表的时候，也应该对relation表的内容做修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public R update(@RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        return R.result(brandService.removeByIds(Arrays.asList(brandIds)), "删除");
    }

}
